package io.litchi.auth.Infrastructure.handler;

import io.litchi.auth.Infrastructure.constant.AuthConstant;
import io.litchi.auth.Infrastructure.util.ResponseUtil;
import io.litchi.common.result.Result;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import java.io.IOException;

/**
 * description: 自定义处理401 未登录
 *
 * @author helang
 * @since 2025-09-11
 */
public class LitchiAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
        Result<Void> result = Result.fail(AuthConstant.UN_LOGIN_MESSAGE);
        ResponseUtil.writeJsonResponse(response, HttpStatus.UNAUTHORIZED.value(), result);
    }
}